package com.blacktea.behavioralpatterns.mediatorpattern;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: black tea
 * @date: 2023/3/25 16:37
 */
@Slf4j
@Service
public class BookService implements Colleague {

    private final Mediator mediator;

    public BookService(@Autowired Mediator mediator) {
        mediator.register(StrUtil.lowerFirst(this.getClass().getSimpleName()), this);
        this.mediator = mediator;
    }

    public void borrowBook(Book book) {
        log.info("book service图书服务借出图书...");
        send("borrowBook", book);
    }

    public void returnBook(Book book) {
        log.info("book service 图书服务收回图书...");
        send("returnBook", book);
    }

    @Override
    public void send(String message, Object... args) {
        mediator.handleMessage(message, this, args);
    }

    @Override
    public void receive(String message) {
        log.info("图书服务接收到消息：" + message);
    }

}
